new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_business_sales_order_detail3",
                Condition: '',
                ExtensionCondition: '',
                Department_SerialNumber: [],
                Sales_Order_SerialNumber: '',
                Sales_Date: null,
                limit: 20,
                page: 1,
            },
            rules: {
                Department_SerialNumber: [{ required: true, message: '请选择甲方', trigger: 'change' }],
            },
            department: [],
            tableData: [],	//表格数据
            loading: false,
        }
    },
    mounted() {
        this.getData();
    },
    computed: {
        Type() {
            return function (str) {
                switch (str) {
                    case "0":
                        return "待发货";
                    case "1":
                        return "发货中";
                    case "2":
                        return "已发货";
                    default:
                        return ""
                }
            }
        }
    },
    methods: {
        onSerach(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.form.page = 1;
                    this.getList(this.form)
                }
                else {
                    this.$message({
                        message: '请选择甲方',
                        type: 'warning'
                    })
                    return false;
                }
            })
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
        },
        getData() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data: {
                    SQLSerialNumber: 'A_business_department_info',
                    Condition: '',
                    ExtensionCondition:'',
                },
                success: function (res) {
                    that.department = res
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        submitForm() {
            const that = this
            if (this.tableData.length > 0) {
                let arr = []
                this.tableData.forEach(item => {
                    //修改状态
                    let Reconciliation_Status = item.Reconciliation_Status.charAt(0);
                    item.Reconciliation_Status = Reconciliation_Status + '1';
                    let is = true;
                    for (let i = 0; i < arr.length; i++) {
                        let obj = arr[i];
                        if (obj.Department_SerialNumber == item.Department_SerialNumber) {
                            obj.Sales_Amount = accAdd(obj.Sales_Amount, item.Amount)
                            obj.Remain_Amount = accAdd(obj.Remain_Amount, item.Amount)
                            obj.Children.business_department_reconciliation_detail.push({
                                Department_Reconciliation_Detail_SerialNumber: '',
                                Department_Reconciliation_SerialNumber: '',
                                Sales_Order_Detail_SerialNumber: item.Sales_Order_Detail_SerialNumber,
                            })
                            is = false;
                            break;
                        }
                    }
                    if (is) {//新
                        arr.push({
                            Department_Reconciliation_SerialNumber: '',
                            Department_SerialNumber: item.Department_SerialNumber,
                            Submit_Date: getYMDHMSDate(),
                            Sales_Amount: item.Amount,
                            Invoice_Amount: 0,
                            Remain_Amount:item.Amount,
                            Row_Status:'0',
                            Children: {
                                business_department_reconciliation_detail: [{
                                    Department_Reconciliation_Detail_SerialNumber: '',
                                    Department_Reconciliation_SerialNumber: '',
                                    Sales_Order_Detail_SerialNumber: item.Sales_Order_Detail_SerialNumber,
                                }]
                            }
                        })
                    }
                });
                $.ajax({
                    headers:requestHeaders(),
                    url: `${BASE_URL2}/generalBusiness/generalSave`,
                    type: 'POST',
                    data: {
                        Data: JSON.stringify({ 
                            Add: { business_department_reconciliation: arr },
                            Modify: { business_sales_order_detail:that.tableData }
                        })
                    },
                    success: function (res) {
                        if (res.Status === 'success') {
                            that.$message.success("提交成功")
                            window.history.go(-1)
                        } else {
                            that.$message.error("提交失败，请稍后再试")
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        that.loading = false
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json"
                })
            } else {
                that.$message({
                    message: '当前没有需要对账的订单明细',
                    type: 'warning'
                })
            }
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            let Sales_Date = ['',''];
            if (data.Sales_Date) {
                Sales_Date = data.Sales_Date
            }
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%")))
            let sql = "";
            this.form.Department_SerialNumber.forEach(item=>{
                if (sql) {
                    sql = sql + " or business_department_info.Department_SerialNumber = '" + item + "'\r\n "
                }else{
                    sql = sql + " business_department_info.Department_SerialNumber = '" + item + "'\r\n "
                }
            })
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(`and (${sql})`)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumberWithOriginal`,
                type: 'POST',
                data,
                success: function (Result) {
                    Result.forEach(item => {
                        let arr = item.Department_Name.split('/')
                        item.Department = [];
                        arr.forEach(i=>{
                            item.Department.push({
                                Department_Name:i.split('_')[0],
                                Department_SerialNumber:i.split('_')[1],
                            })
                        })
                        item.Department_SerialNumber = item.Department[0].Department_SerialNumber
                    })
                    console.log(Result);
                    that.tableData = Result;
                    if (Result.length == 0) {
                        that.$message({
                            message: '未查询到可对账的订单明细',
                            type: 'warning'
                        })
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        handleRemove(index) {
            this.$confirm('是否要删除此行?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = this.tableData.filter((i, id) => id != (this.form.page - 1) * this.form.limit + index);
            }).catch(() => { });
        }
    },
})